/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ufc.quixada.adrs.dao;

import br.ufc.quixada.adrs.dao.config.PostGresMapConfig;
import br.ufc.quixada.adrs.model.Prazo;
import java.sql.SQLException;
import java.util.List;
import java.util.Date;
import java.util.Map;

/**
 *
 * @author Escritorio projetos
 */
public class PrazoDAO {

    public List<Prazo> getAll() throws SQLException {
        return (List<Prazo>) PostGresMapConfig.getSqlMapClient().queryForList("getAllPrazos");
    }

    public List<Prazo> getPrazosDeFimSuperiorAData(Date data) throws SQLException {
        return (List<Prazo>) PostGresMapConfig.getSqlMapClient().queryForList("getPrazosFimAposData", data);
    }

    public List<Prazo> getByAdrs(Long adrsId) throws SQLException {
        return (List<Prazo>) PostGresMapConfig.getSqlMapClient().queryForList("getPrazosByAdrs", adrsId);
    }

    public Prazo getById(Long id) throws SQLException {
        return (Prazo) PostGresMapConfig.getSqlMapClient().queryForObject("getPrazoById", id);
    }

    public void insert(Prazo prazo) throws SQLException {
        prazo.setId(getProxId());
        PostGresMapConfig.getSqlMapClient().insert("addPrazo", prazo);
    }

    public void update(Prazo prazo) throws SQLException {
        PostGresMapConfig.getSqlMapClient().update("updatePrazo", prazo);
    }

    public void updateConfirmacao(Map confirmacaoMap) throws SQLException {
        PostGresMapConfig.getSqlMapClient().update("updateConfirmacao", confirmacaoMap);
    }

    public Boolean getConfirmacao(Map prazoAdrsMap) throws SQLException {
         return (Boolean) PostGresMapConfig.getSqlMapClient().queryForObject("getConfirmacaoByPrazoAndAdrsId", prazoAdrsMap);
    }
    
    public void delete(Prazo prazo) throws SQLException {
        PostGresMapConfig.getSqlMapClient().delete("deletePrazo", prazo);
    }

    public Long getPrazosIniciaisSuperioresAData(Date date) throws SQLException {
        Long id = (Long) PostGresMapConfig.getSqlMapClient().queryForObject("getNumeroPrazosDoAno", date);
        if (id == null) {
            id = 0L;
        }
        return id;
    }

    private Long getProxId() throws SQLException {
        Long id = (Long) PostGresMapConfig.getSqlMapClient().queryForObject("getMaxIdPrazo");
        if (id == null) {
            id = 0L;
        }
        return id + 1L;
    }
}
